using System;using System.Collections.Generic;using System.ComponentModel;using System.Diagnostics;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using HWND = System.IntPtr;namespace SetWindowPosition{ public partial class Form1 : Form { public Form1() { InitializeComponent(); preloadProcesses(); } // Define the FindWindow API function. [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); // Define the SetWindowPos API function. [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags); [DllImport("USER32.DLL")] private static extern IntPtr GetShellWindow(); private delegate bool EnumWindowsProc(HWND hWnd, int lParam); [DllImport("USER32.DLL")] private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam); [DllImport("USER32.DLL")] private static extern bool IsWindowVisible(HWND hWnd); [DllImport("USER32.DLL")] private static extern int GetWindowTextLength(HWND hWnd); [DllImport("USER32.DLL")] private static extern int GetWindowText(HWND hWnd, StringBuilder lpString, int nMaxCount); [DllImport("user32")] private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId); public static Int32 GetWindowProcessID(Int32 hwnd) { Int32 pid = 1; GetWindowThreadProcessId(hwnd, out pid); return pid; } // Define the SetWindowPosFlags enumeration. [Flags()] private enum SetWindowPosFlags : uint { SynchronousWindowPosition = 0x4000, DeferErase = 0x2000, DrawFrame = 0x0020, FrameChanged = 0x0020, HideWindow = 0x0080, DoNotActivate = 0x0010, DoNotCopyBits = 0x0100, IgnoreMove = 0x0002, DoNotChangeOwnerZOrder = 0x0200, DoNotRedraw = 0x0008, DoNotReposition = 0x0200, DoNotSendChangingEvent = 0x0400, IgnoreResize = 0x0001, IgnoreZOrder = 0x0004, ShowWindow = 0x0040, } /// <summary>Returns a dictionary that contains the handle and title of all the open windows.</summary> /// <returns>A dictionary that contains the handle and title of all the open windows.</returns> public static HashSet<WindowInfo> GetOpenWindows() { HWND shellWindow = GetShellWindow(); HashSet<WindowInfo> windows = new HashSet<WindowInfo>(); EnumWindows(delegate (HWND hWnd, int lParam) { if (hWnd == shellWindow) return true; if (!IsWindowVisible(hWnd)) return true; int length = GetWindowTextLength(hWnd); if (length == 0) return true; StringBuilder builder = new StringBuilder(length); GetWindowText(hWnd, builder, length 1); Int32 pid = GetWindowProcessID(hWnd.ToInt32()); Process p = Process.GetProcessById(pid); string appName = p.ProcessName; WindowInfo window = new WindowInfo(hWnd, appName, builder.ToString()); windows.Add(window); return true; }, 0); return windows; } private void preloadProcesses() { HashSet<WindowInfo> allWindows = GetOpenWindows(); foreach (WindowInfo windowData in allWindows) { comboBox1.Items.Add(windowData); } } // Size and position the application. private void btnSet_Click(object sender, EventArgs e) { // Get the target window's handle. IntPtr target_hwnd = //FindWindowByCaption(IntPtr.Zero, txtAppTitle.Text); ((WindowInfo) comboBox1.SelectedItem).hWnd; if (target_hwnd == IntPtr.Zero) { MessageBox.Show( "Could not find a window with the title \"" ((WindowInfo) comboBox1.SelectedItem).title "\""); return; } // Set the window's position. int width = int.Parse(txtWidth.Text); int height = int.Parse(txtHeight.Text); int x = int.Parse(txtX.Text); int y = int.Parse(txtY.Text); SetWindowPos(target_hwnd, IntPtr.Zero, x, y, width, height, 0); } }}
下载<亲测>C# 将其它应用程序设置为当前窗口(可以是qq/微信/钉钉等)用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论